summaryrefslogtreecommitdiffstats
path: root/tcp.c
blob: 4d9418ed8997ed65a3010c3c687dfccf6c895b56 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#pragma once
#include <arpa/inet.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <sys/select.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/types.h>
#include <netdb.h>
#include <string.h>
#include <sys/time.h>
#define ERR_INET_ADDR "0.9.9.0"
#define TCPC_READ_BUF 1024
union ip_conv {
	unsigned char c[4];
	struct in_addr in;
};
struct in_addr hostname_to_ip (const char * hostname) {
	struct hostent *he; /* STATIC! */
	union ip_conv ipconverter;
	struct in_addr *error_addr = malloc(sizeof(struct in_addr));
	/* int ret = */ inet_aton(ERR_INET_ADDR, error_addr); /* TODO: chek for err */
	if ( (he = gethostbyname(hostname)) == NULL ) {
		herror("gethostbyname");
		return *error_addr;
	} else {
		memcpy(ipconverter.c, he->h_addr, 4); // fuck yes now it works
		// fprintf(stderr, "debug: %s\n", inet_ntoa(ipconverter.in));
		return ipconverter.in;
	}
}
int spawn_conn (const char * address, const int port) {
	int ret;
	int conn_fd;
	struct sockaddr_in server_addr = { 0 };
	server_addr.sin_family = AF_INET;
	server_addr.sin_port = htons(port);
	ret = inet_pton(AF_INET, address, &server_addr.sin_addr);
	if (ret != 1) {
		if (ret == -1)
			perror("inet_pton");
		fprintf(stderr, "%s is not an IPv4, trying to resolve ...\n", address);
		struct in_addr ret = hostname_to_ip(address);
		struct in_addr error_addr;
		inet_aton(ERR_INET_ADDR, &error_addr);
		if (memcmp(&ret, &error_addr, 4) == 0) {
			fprintf(stderr, "failed to resolve.\n");
			return -1;
		}
		server_addr.sin_addr = ret;
		fprintf(stderr, "resolved to  %s.\n", inet_ntoa(server_addr.sin_addr));
	}
	conn_fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
	if(conn_fd == -1) {
		perror("socket");
		return -1;
	}
	ret = connect(conn_fd, (struct sockaddr*)&server_addr, sizeof(server_addr));
	if (ret == 1) {
		perror("connect");
		return -1;
	}
	return conn_fd;
}

int kill_conn (int conn_fd) {
	int ret = shutdown(conn_fd, SHUT_RDWR); // preprečimo tako read kot write.:wq
	if (ret == -1) {
		perror("shutdown");
		return -1;
	}
	ret = close(conn_fd);
	if (ret == -1) {
		perror("close");
		return -1;
	}
	return 0;
}

int read_until(int conn_fd, FILE * out, unsigned int timeout, const char * ma,
		unsigned long long int max_bytes) {
	int ret = 0;
	unsigned int match = 0;
	struct timeval start, stop;
	gettimeofday(&start, NULL);
	char c[TCPC_READ_BUF+1];
	while (1) {
		ret = read(conn_fd, c, ma ? 1 : TCPC_READ_BUF);
		if (ret == -1) {
			if (errno == EWOULDBLOCK) {
			} else {
				fprintf(stderr, "%s@" __FILE__ ":%d read(): %s%d\n", __func__, __LINE__, strerror(errno), ret);
				return 1;
			}
		} else if (ret == 0) { /* strežnik ni poslal ničesar */
			fprintf(stderr, "%s@" __FILE__ ":%d read(): server closed connection\n", __func__, __LINE__);
			return 0;
		} else {
			fwrite(c, ret, 1, out);
			max_bytes--;
			if (max_bytes <= 0) {
				return 0;
			}
			if (ma != NULL) {
				if (ma[match] == c[0]) {
					match++;
					if (match == strlen(ma)) {
						return 0;
					}
				} else {
					match = 0;
				}
			}
		}
		gettimeofday(&stop, NULL);
		if (stop.tv_sec - start.tv_sec > timeout) {
			fprintf(stderr, "%s@" __FILE__ ":%d E_TIMEOUT %ld-%ld>%u\n", __func__,
					__LINE__, stop.tv_sec, start.tv_sec, timeout);
			return 2;
		}
	}
	return 0;
}

int sync_write(int conn_fd, const char * req, int len, unsigned int timeout) {
	int ret = write(conn_fd, req, len);
	struct timeval start, stop;
	gettimeofday(&start, NULL);
	if (ret == -1) {
		if (errno == EBADF) {
			fprintf(stderr, "tcp.c: sync_write: write EBADF: %s\n", strerror(errno));
			return -1;
		}
		while (errno == EWOULDBLOCK) {
			ret = write(conn_fd, req, len);
			if(ret != -1) {
				return 0;
			}
			gettimeofday(&stop, NULL);
			if (stop.tv_sec - start.tv_sec > timeout) {
				fprintf(stderr, "tcp.c: sync_write: E_TIMEOUT %ld-%ld>%u\n",
						start.tv_sec, stop.tv_sec, timeout);
				return -2;
			}
		}
	}
	return 0;
}


#if __INCLUDE_LEVEL__ == 0
	static volatile int sigint = 0;
	void intHandler(int sig) {
		signal(sig, SIG_IGN);
		if (sig == SIGINT)
			sigint++;
		if (sigint >= 3) // some people smash CtrlC multiple times to force quit!
			exit(130);
		signal(sig, intHandler);
	}
	int main (int argc, char ** argv) {
		if (argc != 1+2) {
			fprintf(stderr, "usage: %s ip.v4.ad.dr port\n", argv[0]);
			return 1;
		}
		signal(SIGINT, intHandler);
		#define ADDRESS_ARG argv[1]
		#define PORT_ARG argv[2]
		int conn_fd = spawn_conn(ADDRESS_ARG, atoi(PORT_ARG));
		if (conn_fd < 0) {
			fprintf(stderr, "error connecting!\n");
			return 2;
		} else {
			fprintf(stderr, "suc. conn with fd %d\n\n", conn_fd);
		}
		int buf = 0;
		#define READ_MAX_SIZE 1024
		char read_buf[READ_MAX_SIZE];
		int i = 0;
		char input;
		fcntl(STDIN_FILENO, F_SETFL, fcntl(STDIN_FILENO, F_GETFL) | O_NONBLOCK);
		while (1) {
			if (sigint > 0) {
				// fprintf(stderr, "\n" USERSTRING_EXIT_CONFIRM " (y/N)\n");
				// char gotchar = getchar();
				// if (gotchar == 'y' || gotchar == 'Y' || gotchar == 'd' ||
				// 	gotchar == 'D' || gotchar == 'j' || gotchar == 'J') {
				// 	kill_conn(conn_fd);
				// 	return 0;
				// }
				if (kill_conn(conn_fd) == 0) {
					fprintf(stderr, "\nconnection killed successfully, exiting ...\n");
				} else {
					fprintf(stderr, "\nconnection killing FAILED, exiting ...\n");
				}
				return 0;
			}
			buf = read(conn_fd, read_buf, READ_MAX_SIZE);
			if (buf == -1 && errno != EWOULDBLOCK) {
				fprintf(stderr, "\nerror reading from socket.\n");
				if (kill_conn(conn_fd) != 0) {
					fprintf(stderr, "\nerror killing socket\n");
					return 3;
				}
				return 4;
			}
			if (buf == 0) {
				fprintf(stderr, "\nserver closed socket\n");
				if (kill_conn(conn_fd) != 0) {
					fprintf(stderr, "\nerror killing socket\n");
					return 5;
				}
				return 6;
			}
			if (errno == EWOULDBLOCK && buf == -1) {
				// no data is on socket; sockets are non blocking.
			} else {
				buf = write(STDOUT_FILENO, read_buf, buf);
				if(buf == -1) {
					fprintf(stderr, "\nerror writing to stdout\n");
					return 7;
				}
			}
			buf = read(STDIN_FILENO, read_buf, READ_MAX_SIZE);
			if (buf == -1 && errno != EWOULDBLOCK) {
				fprintf(stderr, "\nerror reading from stdin\n");
				return 8;
			}
			if (buf == 0) {
				// fprintf(stderr, "\neof on stdin\n"); // that's okay
				// return 9;
			}
			if (errno == EWOULDBLOCK && buf == -1) {
				// no data in stdin
			} else {
				i = write(conn_fd, read_buf, buf);
				if (i == -1) {
					if(errno != EWOULDBLOCK) {
						fprintf(stderr, "\nerror writing to socket\n");
						if(kill_conn(conn_fd) != 0) {
							fprintf(stderr, "\nerror killing connection\n");
							return 10;
						}
						return 12;
					} else {
						while (i == -1) {
							if (errno == EWOULDBLOCK) {
								fprintf(stderr, "\nwrite to socket blocked, trying again\n");
								i = write (conn_fd, read_buf, buf);
							} else {
								fprintf(stderr, "\nerror writing to socket\n");
								if (kill_conn(conn_fd) != 0) {
									fprintf(stderr, "\nerror killing connection\n");
									return 13;
								}
								return 14;
							}
						}
					}
				}
			}
			buf = 0;
		}
	}
#endif